//
//  SZLChargeListViewController.m
//  Shengzhuli
//
//  Created by 卡忙科技 on 16/8/19.
//  Copyright © 2016年 kamangkeji. All rights reserved.
//

#import "SZLChargeListViewController.h"
#import "ChargeListCell.h"
#import "BuyListCell.h"
#import "SZLAccountHelper.h"
#import "MBProgressHUD+MJ.h"
#import <MJRefresh.h>
#import "WithDrawListCell.h"

@interface SZLChargeListViewController ()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic,assign) NSInteger curPage;

@end

@implementation SZLChargeListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setTableView];
    self.curPage = 1;
    if (self.type == RecoredTypeCharge) {
        self.navigationItem.title = @"充值记录";
        [self getSourcesList];
    }else if(self.type == RecoredTypeBuy){
        self.navigationItem.title = @"消费记录";
        [self getSourcesList];
    }else if (self.type == RecoredTypeWithDraw){
        self.navigationItem.title = @"提现记录";
        [self getWithDrawLsit];
    }
}
- (void)setTableView
{
    [self.sourceTab registerNib:[UINib nibWithNibName:NSStringFromClass([ChargeListCell class]) bundle:nil] forCellReuseIdentifier:NSStringFromClass([ChargeListCell class])];
    [self.sourceTab registerNib:[UINib nibWithNibName:NSStringFromClass([BuyListCell class]) bundle:nil] forCellReuseIdentifier:NSStringFromClass([BuyListCell class])];
    [self.sourceTab registerNib:[UINib nibWithNibName:NSStringFromClass([WithDrawListCell class]) bundle:nil] forCellReuseIdentifier:NSStringFromClass([WithDrawListCell class])];
    self.sourceTab.tableFooterView = [[UIView alloc]init];
    __unsafe_unretained typeof(self) weakSelf = self;
    self.sourceTab.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        weakSelf.curPage++;
        if (self.type == RecoredTypeBuy) {
            [weakSelf getWithDrawLsit];
        }else{
            [weakSelf getSourcesList];
        }
    }];
    [self.view addSubview:self.sourceTab];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (self.type == RecoredTypeCharge) {
        return 100;
    }else if (self.type == RecoredTypeBuy){
        return 160;
    }
    ChargeListModel *model = self.sources[indexPath.row];
    float reasonH = [model.rejectReason boundingRectWithSize:CGSizeMake(ScreenW-20, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:Font(16)} context:nil].size.height;
    return 180+reasonH+10;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.sources.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (self.type == RecoredTypeBuy) {
        BuyListCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([BuyListCell class])];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.model = self.sources[indexPath.row];
        return cell;
    }else if (self.type == RecoredTypeCharge){
        ChargeListCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([ChargeListCell class])];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.model = self.sources[indexPath.row];
        return cell;
    }
    WithDrawListCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([WithDrawListCell class])];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.clipsToBounds = YES;
    cell.model = self.sources[indexPath.row];
    return cell;
}
#pragma mark --------------消费充值记录----------------
- (void)getSourcesList
{
    NSMutableDictionary *parame = [NSMutableDictionary dictionary];
    parame[@"token"] = [SZLAccountHelper shareInstance].token;
    parame[@"pageNo"] = @(self.curPage);
    [self showWaiter];
    [MyCountNetwork getBuyChargeListWithParame:parame type:self.type response:^(BOOL success, NSString *msg, NSArray *lists) {
        [self hideWaiter];
        if (success) {
            [self hideRequestAgainView];
            [self.sourceTab.mj_footer endRefreshing];
            if (lists.count == 0&&self.curPage==1) {
                NSString *tips = (self.type == RecoredTypeBuy)?@"暂无消费记录":@"暂无充值记录";
                [self showNoDataView:self.view.frame title:tips];
                return ;
            }else if (lists == 0&&self.curPage>1){
                [MBProgressHUD showMessage:@"没有更多记录了" toView:self.view];
                self.sourceTab.mj_footer.hidden = YES;
                return;
            }
            self.sourceTab.mj_footer.hidden = (lists.count<20)?YES:NO;
            [self.sources addObjectsFromArray:lists];
            [self.sourceTab reloadData];
        }else{
            if ([msg isEqualToString:NoNetworkStatus]) {
                [self showRequestAgainView:@selector(getSourcesList)];
            }
            [MBProgressHUD showError:msg toView:self.view];
        }
    }];
}
- (void)getWithDrawLsit
{
    NSMutableDictionary *parame = [NSMutableDictionary dictionary];
    parame[@"token"] = [SZLAccountHelper shareInstance].token;
    parame[@"pageNo"] = @(self.curPage);
    [self showWaiter];
    [MyCountNetwork getWithDrawListWithParame:parame response:^(BOOL success, NSString *msg, NSArray *lists) {
        [self hideWaiter];
        if (success) {
            [self hideRequestAgainView];
            [self.sourceTab.mj_footer endRefreshing];
            if (lists.count == 0&&self.curPage==1) {
                [self showNoDataView:self.view.frame title:@"暂无提现记录"];
                return ;
            }else if (lists == 0&&self.curPage>1){
                [MBProgressHUD showMessage:@"没有更多记录了" toView:self.view];
                self.sourceTab.mj_footer.hidden = YES;
                return;
            }
            self.sourceTab.mj_footer.hidden = (lists.count<20)?YES:NO;
            [self.sources addObjectsFromArray:lists];
            [self.sourceTab reloadData];
        }else{
            if ([msg isEqualToString:NoNetworkStatus]) {
                [self showRequestAgainView:@selector(getSourcesList)];
            }
            [MBProgressHUD showError:msg toView:self.view];
        }
    }];
}

@end
